home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / news / bsnews.arc / PN.C < prev    next >
C/C++ Source or Header  |  1989-08-23  |  5KB  |  239 lines

  1. /*
  2.  * pn.c - postnews command for bootstrap news
  3.  * copyright 1989 Ronald Florence (ron@mlfarm 7/9/89)
  4.  *
  5.  * Originally written as a shell script in the original bsnews
  6.  * package.  Re-written as a C program for use with MT C-Shell
  7.  * by David Beckemeyer (david@bdt 8/23/89)
  8.  * 
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <osbind.h>
  13. #include <types.h>
  14. #include <time.h>
  15.  
  16. extern char *ctime();
  17. extern char **environ;
  18.  
  19. extern char *feedhost();
  20.  
  21. #define SEQFILE    "\\usr\\lib\\news\\seq"
  22. #define ORGFILE    "\\usr\\lib\\news\\org"
  23. #define TMPART "\\tmp\\newsart"
  24.  
  25. #define EDITOR    "\\usr\\bin\\me.prg"
  26. #define UUXPRG    "\\usr\\bin\\uux.prg"
  27.  
  28. char *seqfile = SEQFILE;
  29. char *tmpart = TMPART;
  30. char *ctlfile = "\\usr\\spool\\uucp\\uucp.ctl";
  31.  
  32. char line[512];
  33.  
  34. char *lname()
  35. {
  36.     FILE *fp;
  37.     char *s;
  38.  
  39.     if ((fp = fopen(ctlfile, "r")) == 0) {
  40.         fprintf(stderr, "cannot open %s\n", ctlfile);
  41.         exit(1);
  42.     }
  43.     while (fgets(line, 512, fp)) {
  44.         if (strncmp(line, "nodename", 8) == 0) {
  45.             for (s = line+9; *s > ' '; s++)
  46.                 ;
  47.             *s = 0;
  48.             return(line+9);
  49.         }
  50.     }
  51.     fclose(fp);
  52.     return(0);
  53. }
  54.  
  55.  
  56. char *getorg()
  57. {
  58.     FILE *fp;
  59. static char orgbuf[80];
  60.  
  61.     if ((fp = fopen(ORGFILE, "r")) == 0) {
  62.         printf("cannot open %s\n", ORGFILE);
  63.         exit(1);
  64.     }
  65.     if (!fgets(orgbuf, 80, fp)) {
  66.         printf("cannot read %s\n", ORGFILE);
  67.         exit(1);
  68.     }
  69.     fclose(fp);
  70.     orgbuf[strlen(orgbuf)-1] = 0;
  71.     return(orgbuf);
  72. }
  73.  
  74. seq()
  75. {
  76.     FILE *fp;
  77.     char buf[32];
  78.     int x;
  79.  
  80.     if ((fp = fopen(seqfile, "r")) == 0) {
  81.         fprintf(stderr, "cannot open %s\n", seqfile);
  82.         exit(1);
  83.     }
  84.     if (fgets(buf, 32, fp) == 0)
  85.         x = 0;
  86.     else
  87.         x = atoi(buf);
  88.     fclose(buf);
  89.     return(x);
  90. }
  91.  
  92. incr()
  93. {
  94.     int x;
  95.     FILE *fp;
  96.  
  97.     x = seq();
  98.  
  99.     if ((fp = fopen(seqfile, "w")) == 0) {
  100.         fprintf(stderr, "cannot open %s\n", seqfile);
  101.         exit(1);
  102.     }
  103.     fprintf(fp, "%d\n", x+1);
  104.     fclose(fp);
  105. }
  106.  
  107. static char *edtargv[] = {
  108.     EDITOR,
  109.     TMPART,
  110.     0
  111. };
  112.  
  113. editit()
  114. {
  115.     execve(EDITOR, edtargv, environ);
  116. }
  117.  
  118. static char uuxcmd[64];
  119.  
  120. static char *uuxargv[] = {
  121.     "uux", "-", uuxcmd, 0
  122. };
  123.  
  124. main()
  125. {
  126.     FILE *tmpfp;
  127.     char groups[80];
  128.     char subject[80];
  129.     char yn[10];
  130.     char *sys, *user, *getenv();
  131.     time_t t;
  132.  
  133.     if ((user = getenv("USER")) == 0) {
  134.         fprintf(stderr, "USER undefined\n");
  135.         exit(1);
  136.     }
  137.     if ((sys = lname()) == 0) {
  138.         fprintf(stderr, "nodename undefined\n");
  139.         exit(1);
  140.     }
  141.     
  142.     printf("Enter Newsgroups: ");
  143.     fflush(stdout);
  144.     gets(groups);
  145.     printf("Enter Subject: ");
  146.     fflush(stdout);
  147.     gets(subject);
  148.  
  149.     time(&t);
  150.     tmpfp = fopen(tmpart, "w");
  151.     if (tmpfp == 0) {
  152.         fprintf(stderr, "cannot open %s\n", tmpart);
  153.         exit(1);
  154.     }
  155.     fprintf(tmpfp, "Path: %s!%s\n", sys, user);
  156.     fprintf(tmpfp, "From: %s@%s.UUCP\n", user, sys);
  157.     fprintf(tmpfp, "Newsgroups: %s\n", groups);
  158.     fprintf(tmpfp, "Subject: %s\n", subject);
  159.     fprintf(tmpfp, "Message-ID: <%d@%s.UUCP>\n", seq(), sys);
  160.     fprintf(tmpfp, "Date: %s", ctime(&t));
  161.     fprintf(tmpfp, "Followup-To: \n");
  162.     fprintf(tmpfp, "Distribution:\n");
  163.     fprintf(tmpfp, "Keywords: \n");
  164.     fprintf(tmpfp, "Organization: %s\n", getorg());
  165.     fprintf(tmpfp, "Lines: \n\n\n");
  166.     fclose(tmpfp);
  167.  
  168.     printf("Invoking editor...\n");
  169.     fflush(stdout);
  170.  
  171.     editit();
  172.  
  173.     printf("Post to all of usenet? ");
  174.     fflush(stdout);
  175.     gets(yn);
  176.     if (yn[0] == 'y') {
  177.         printf("posting article...\n");
  178.         sprintf(uuxargv[2], "%s!rnews", feedhost());
  179.         invoke(UUXPRG, uuxargv, tmpart, "");
  180.         incr();
  181.     }
  182.     else
  183.         printf("cancelled\n");
  184.     exit(0);
  185. }
  186.  
  187.  
  188. extern char *environ;
  189. extern int errno;
  190. extern char *_iovector;
  191.  
  192. /* Invoke the local program. Return its status (0 == ok) or -1 */
  193. invoke(file, argv, input, output)
  194. char *file, *argv[], *input, *output;
  195. {
  196.     char tmp[128];
  197.     int fd0, fd1, std0, std1, err;
  198.     char env[128], *p;
  199.  
  200.     /* save the MWC io setup */
  201.     strcpy(env, _iovector);
  202.     if (strlen(input)) {
  203.     strcpy(tmp, input);
  204.     std0 = Fdup(0);
  205.     fd0 = Fopen(tmp, 0);
  206.     if (fd0 > 0) {
  207.         _iovector[0] = 'F';
  208.         Fforce(0, fd0);
  209.     }
  210.         else
  211.         printf("cannot open %s as stdin\n", tmp);
  212.     }
  213.     if (strlen(output)) {
  214.     strcpy(tmp, output);
  215.     std1 = Fdup(1);
  216.     fd1 = Fopen(tmp, 0);
  217.     if (fd1 > 0) {
  218.         _iovector[1] = 'F';
  219.         Fforce(1, fd1);
  220.     }
  221.         else
  222.         printf("cannot open %s as stdout\n", tmp);
  223.     }
  224.     err = execve(file, argv, environ);
  225.     if (strlen(input)) {
  226.     Fclose(fd0);
  227.     Fforce(0, std0);
  228.     }
  229.     if (strlen(output)) {
  230.     Fclose(fd1);
  231.     Fforce(1, std1);
  232.     }
  233.    /* restore MWC io for stdio calls */
  234.     strcpy(_iovector, env);
  235.     if (err)
  236.     printf("exec failed %d (errno=%d)\n", err, errno);
  237.     return(err);
  238. }
  239.